<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Set extends Model
{
    protected $table = 'set';
    
    /**
     * 是否主动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;
    
    public static function getSet($field = ''){
        if (empty($field)) {
            $set = Set::get()->toArray();
            if (!$set) return [];
            $sets = [];
            foreach ($set as $v) {
                $sets[$v['name']] = json_decode($v['value'],true);
            }
            return $sets;
        }
        $set = Set::where("name",$field)->first();
        if (!$set) return [];
        return json_decode($set->value,true);
    }
    
    public static function setSet($field,$data){
        $set = Set::where("name",$field)->first();
        if (!$set) Set::insert(array('name'=>$field,'value'=>json_encode($data)));
        Set::where("name",$field)->update(array('value'=>json_encode($data)));
    }
}
